package com.our.sys.project1.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.our.sys.project1.dao.FlowPathDao;
import com.our.sys.project1.dao.KnowledgeDao;
import com.our.sys.project1.dao.RuleDao;
import com.our.sys.project1.entity.FlowPath;
import com.our.sys.project1.entity.Knowledge;
import com.our.sys.project1.entity.Rule;

@Service
@Transactional
public class TBusinessService {

    private static Logger logger = LoggerFactory.getLogger(TBusinessService.class);

    @Autowired
    private FlowPathDao flowPathDao;
    @Autowired
    private KnowledgeDao knowledgeDao;
    @Autowired
    private RuleDao ruleDao;

    @Transactional(readOnly = true)
    public String getFlowPathContent() {
        FlowPath fp = flowPathDao.findUnique("from FlowPath");
        return fp == null ? "" : fp.getContent();
    }

    @Transactional(readOnly = true)
    public String getKnowledgeContent(Long mId) {
        Knowledge knowledge = knowledgeDao.findUnique("from Knowledge where mId = ?", mId);
        return knowledge == null ? "" : knowledge.getContent();
    }

    @Transactional(readOnly = true)
    public String getRuleContent(Long tId) {
        Rule rule = ruleDao.findUnique("from Knowledge where tId = ?", tId);
        return rule == null ? "" : rule.getContent();
    }

    public void saveFlowPath(String content) throws Exception {
        try {
            FlowPath fp = flowPathDao.findUnique("from FlowPath");
            if (null != fp) {
                fp.setContent(content);
                flowPathDao.save(fp);
            } else {
                FlowPath newFp = new FlowPath();
                newFp.setContent(content);
                flowPathDao.save(newFp);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveKnowledge(Long mId, String content) throws Exception {
        try {
            Knowledge knowledge = knowledgeDao.findOneBy("mId", mId);
            if (null != knowledge) {
                knowledge.setContent(content);
                knowledgeDao.save(knowledge);
            } else {
                Knowledge newKnowledge = new Knowledge();
                newKnowledge.setmId(mId);
                newKnowledge.setContent(content);
                knowledgeDao.save(newKnowledge);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveRule(Long tId, String content) throws Exception {
        try {
            Rule rule = ruleDao.findOneBy("tId", tId);
            if (null != rule) {
                rule.setContent(content);
                ruleDao.save(rule);
            } else {
                Rule newRule = new Rule();
                newRule.settId(tId);
                newRule.setContent(content);
                ruleDao.save(newRule);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteFlowPath() throws Exception {
        try {
            flowPathDao.batchExecute("delete from FlowPath");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteKnowledge(Long id) throws Exception {
        try {
            knowledgeDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteRule(Long id) throws Exception {
        try {
            ruleDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
